package com.situ.stl.fore;

import com.situ.stl.constants.Constants;
import com.situ.stl.entity.Course;
import com.situ.stl.entity.Member;
import com.situ.stl.entity.Reserve;

import com.situ.stl.service.MemberService;
import com.situ.stl.service.ReserveService;
import com.situ.stl.util.JWTUtil;
import com.situ.stl.util.Result;
import com.situ.stl.vo.ReserveVo;
import io.jsonwebtoken.Claims;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Controller层 - 预约
 */
@RestController("/foreReserveController")
@RequestMapping("/fore")
@Slf4j
public class ReserveController {

    /**
     */
    @Autowired
    private ReserveService reserveService;
    @Autowired
    private MemberService memberService;

    /**
     * add
     */
    @RequestMapping("/reserve")
    public Result add(@RequestBody Reserve reserve, HttpServletRequest request) {
        // 读取请求头中的Token
        String token = request.getHeader("token");

        // 当前登录用户
        Member member = null;
        // 有Token, 解析
        if ( StringUtils.hasText(token)) {
            // 尝试解析
            try {
                Claims claims = JWTUtil.parseJWT(token);
                String phone = claims.getSubject();
                member = memberService.getByPhone(phone);
            } catch (Exception e) {
                log.info("无效的token");
            }
        }

        try {
            if (member != null) {
                reserve.setMemberId(member.getId());
            } else {
                throw new Exception("会员未登录！");
            }
            reserveService.addReserve(reserve);
            return Result.success(Constants.STATUS_200);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(Constants.STATUS_10001,e.getMessage());
        }
    }
    @GetMapping("/reservations/{memberId}")
    public Result getReservationsByMemberId(@PathVariable Integer memberId) {

        try {
            // 查询该用户的预约记录
            List<ReserveVo> reservations = reserveService.getReservationsByMemberId1(memberId);
            System.out.println("reservations = " + reservations);
            return Result.success(Constants.STATUS_200, reservations);
        } catch (Exception e) {

            return Result.error(Constants.STATUS_10001, "查询预约记录失败");
        }
    }
    /**
     * remove
     */
    @RequestMapping("/reserve1/{id}")
    public Result remove(@PathVariable Integer id,@RequestBody Reserve reserve) {

        try {
            Integer memberId = reserve.getMemberId();
            Reserve existingReserve = reserveService.searchByCourseIdAndMemberId(id, memberId);
            if (existingReserve == null) {
                return Result.error(Constants.STATUS_10001, "未找到该用户的预约记录");
            }
            existingReserve.setStatus(0); // 设置为已取消状态
            int row = reserveService.modifyReserve2(existingReserve);

            if (row == 0) {
                return Result.error(Constants.EDIT_RESERVE_ERROR, "取消预约失败");
            }
        } catch (Exception e) {
            log.info("取消失败");
            return Result.error(Constants.STATUS_10001, "未预约的课程无法取消");
        }
        return Result.success(Constants.STATUS_200);
    }
    @RequestMapping("/reserve2")
    public Result edit(@RequestBody Reserve reserve, HttpServletRequest request) {
        // 读取请求头中的Token
        String token = request.getHeader("token");

        // 当前登录用户
        Member member = null;
        // 有Token, 解析
        if ( StringUtils.hasText(token)) {
            // 尝试解析
            try {
                Claims claims = JWTUtil.parseJWT(token);
                String phone = claims.getSubject();
                member = memberService.getByPhone(phone);
            } catch (Exception e) {
                log.info("无效的token");
            }
        }

        try {
            if (member != null) {
                reserve.setMemberId(member.getId());
            } else {
                throw new Exception("会员未登录！");
            }
            reserveService.modifyReserve5(reserve);
            return Result.success(Constants.STATUS_200);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(Constants.STATUS_10001,e.getMessage());
        }
    }



}
